package protocol.work.connection.http.wlwx;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import protocol.common.entity.ConnectionWorkState;
import protocol.common.entity.HttpConnConfig;
import protocol.common.entity.HttpParametersConfig;
import protocol.common.entity.ParametersType;
import protocol.common.httpclient.HttpRequestManager;
import protocol.common.httpclient.response.HttpResponseDefaultHandler;
import protocol.common.util.MD5Encrypt;
import protocol.work.connection.http.AbstractHandleWork;
import protocol.work.connection.http.IConnectionWork;
import redis.clients.jedis.Jedis;

public class SendWorkHandle extends AbstractHandleWork implements IConnectionWork{

	private static Map<String,String> submitDescMap;
	
	private HttpParametersConfig sendConfig;
	
	private String[] cacheKey;
	
	public SendWorkHandle(HttpConnConfig httpConnConfig) {
		super(httpConnConfig.getChannle());
		this.cacheKey = new String[]{httpConnConfig.getSendCacheKey(),httpConnConfig.getReportCacheKey()};
		this.sendConfig = httpConnConfig.getParametersConfig(ParametersType.SEND);
	}
	
	static{
		submitDescMap = new HashMap<String,String>();
		submitDescMap.put("Error-0","发送成功");
		submitDescMap.put("Error-13","目标号码在黑名单中");
		submitDescMap.put("Error-15","通道不支持该目标号码");
		submitDescMap.put("Error-8","流量超过限制");
		submitDescMap.put("Error-29","无效号码");
	}
	
	@Override
	public ConnectionWorkState handle(Jedis jedis,HttpRequestManager httpRequestManager) throws Exception {
		Map<String,String> sendMap = this.pollSendSmsInfo(jedis);
		if(sendMap!=null){
			HttpResponseDefaultHandler responseHandler  = httpRequestManager.post(sendConfig.getConfig("url"),sendMap);
			String mapKey = sendMap.get("mobileId");
			Map<String,String> submitReponseMap = this.readResponseStr(responseHandler.getResponseString(),mapKey);
			String redisKey = "SubmitResponse-"+mapKey;
			jedis.hmset(redisKey,submitReponseMap);
			jedis.lpush(cacheKey[1], redisKey);
			return ConnectionWorkState.SUCCESS;
		}else{
			return ConnectionWorkState.FREE; 
		}
	}
	
	private Map<String,String> readResponseStr(String reponse,String mobileId) throws UnsupportedEncodingException{
		Map<String,String> submitReponseMap = new HashMap<String,String>();
		submitReponseMap.put("MobileId",mobileId);
		submitReponseMap.put("DataType","Submit");
		String[] reponseItems = reponse.split("\r\n");
		String[] submitState = reponseItems[0].split(":");
		if(submitState[0].equals("SUCCESS")){
			String[] mobileState = reponseItems[1].split(":");
			submitReponseMap.put("SubmitStatus",mobileState[2]);
			String submitDesc = mobileState[2].equals("0")?submitState[1]:submitDescMap.get("Error-"+mobileState[2]);
			submitReponseMap.put("SubmitDesc",submitDesc==null ? "":submitDesc);
			submitReponseMap.put("MsgId",mobileState[1]);
		}else{
			submitReponseMap.put("MsgId","SubmitError-"+mobileId);
			submitReponseMap.put("SubmitStatus","99");
			submitReponseMap.put("SubmitDesc",URLDecoder.decode(submitState[1],"utf-8"));
		}
		submitReponseMap.put("MsgSendState",submitReponseMap.get("SubmitStatus").equals("0")?"13":"14");
		return submitReponseMap;
	}
	
	private Map<String,String> pollSendSmsInfo(Jedis jedis) throws UnsupportedEncodingException{
		Map<String,String> smsReturnMap = new HashMap<String,String>();
		String mapKey = jedis.lpop(cacheKey[0]);
		if(mapKey == null) return null;
		Map<String,String> smsMap = jedis.hgetAll(mapKey);
		smsReturnMap.put("cust_code",sendConfig.getConfig("custCode"));
		smsReturnMap.put("sp_code",smsMap.get("SRC_NUMBER"));
		String content = smsMap.get("MSG_SIGNATURE") + smsMap.get("MSG_CONTENT");
		smsReturnMap.put("content",content);
		smsReturnMap.put("destMobiles",smsMap.get("MOBILE"));
		String msgContentEncoder = URLEncoder.encode(content,"utf-8");
		String sign = MD5Encrypt.sign(msgContentEncoder, sendConfig.getConfig("custCode"), "utf-8");
		smsReturnMap.put("sign",sign);
		smsReturnMap.put("mobileId",smsMap.get("MOBILE_ID"));
		return smsReturnMap;
		
	}
}
